package org.huel.backend.mapper;

import org.apache.ibatis.annotations.*;
import org.huel.backend.domain.entity.ClassAttendance;

import java.util.List;

@Mapper
public interface ClassAttendanceMapper {
    /**
     * 获取出勤人数最高的前N个班级
     * @param limit 限制返回的记录数
     * @return 出勤人数最高的班级列表
     */
    @Select("SELECT * FROM class_attendance ORDER BY present_count DESC LIMIT #{limit}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "classId", column = "class_id"),
            @Result(property = "presentCount", column = "present_count"),
            @Result(property = "absentCount", column = "absent_count"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    List<ClassAttendance> selectTopPresentClasses(@Param("limit") int limit);

    /**
     * 获取缺勤人数最高的前N个班级
     * @param limit 限制返回的记录数
     * @return 缺勤人数最高的班级列表
     */
    @Select("SELECT * FROM class_attendance ORDER BY absent_count DESC LIMIT #{limit}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "classId", column = "class_id"),
            @Result(property = "presentCount", column = "present_count"),
            @Result(property = "absentCount", column = "absent_count"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    List<ClassAttendance> selectTopAbsentClasses(@Param("limit") int limit);

    /**
     * 根据班级ID查询班级出勤情况
     * @param classId 班级ID
     * @return 班级出勤记录
     */
    @Select("SELECT * FROM class_attendance WHERE class_id = #{classId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "classId", column = "class_id"),
            @Result(property = "presentCount", column = "present_count"),
            @Result(property = "absentCount", column = "absent_count"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    ClassAttendance selectByClassId(@Param("classId") String classId);

    /**
     * 插入新的班级出勤记录
     * @param classAttendance 班级出勤实体
     * @return 影响的行数
     */
    @Insert("INSERT INTO class_attendance (class_id, present_count, absent_count) VALUES (#{classId}, #{presentCount}, #{absentCount})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertClassAttendance(ClassAttendance classAttendance);

    /**
     * 更新班级出勤记录
     * @param classAttendance 班级出勤实体
     * @return 影响的行数
     */
    @Update("UPDATE class_attendance SET present_count = #{presentCount}, absent_count = #{absentCount} WHERE id = #{id}")
    int updateClassAttendance(ClassAttendance classAttendance);
}